package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.CustomerMapper;
import com.jt.pojo.Customer;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService{
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public PageResult getCustomerList(PageResult pageResult) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasLength(pageResult.getQuery()), "username", pageResult.getQuery());

        IPage<Customer> page = new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        page = customerMapper.selectPage(page,queryWrapper);
        long total = page.getTotal();
        List<Customer> rows = page.getRecords();
        return pageResult.setTotal(total).setRows(rows);
    }

    @Override
    @Transactional
    public void addCustomer(Customer customer) {
        customerMapper.insert(customer);
    }

    @Override
    @Transactional
    public void updateCustomer(Customer customer) {
        customer.setUpdated(new Date());
        customerMapper.updateById(customer);

    }

    @Override
    @Transactional
    public void deleteCustomer(Integer id) {
        customerMapper.deleteById(id);
    }

    @Override
    public Customer getCustomerMessage(Integer id) {
        return customerMapper.selectById(id);
    }
}
